_package("alz.gesture");

_import("alz.gesture.Touch");

/**
 * 拖动手势
 */
_class("Drag", Touch, function(){
	this.handles = ["dragstart", "drag", "dragend"];
	this.dragThreshold = 5;
	this.direction = "both";
	this.horizontal = false;
	this.vertical = false;
	this._init = function(config){
		_super._init.apply(this, arguments);
		if(this.direction == "both"){
			this.horizontal = true;
			this.vertical = true;
		}else if(this.direction == "horizontal"){
			this.horizontal = true;
		}else{
			this.vertical = true;
		}
		return this;
	};
	this.onTouchStart = function(ev, touch){
		this.startX = this.previousX = touch.pageX;
		this.startY = this.previousY = touch.pageY;
		this.startTime = this.previousTime = ev.timeStamp;
		this.dragging = false;
	};
	this.onTouchMove = function(ev, touch){
		if(this.isLocked("drag")){
			return;
		}
		var info = this.getInfo(touch);
		if(!this.dragging){
			if(this.isDragging(info) && this.fire("dragstart", ev, info)){
				this.dragging = true;
				this.lock("drag", "dragstart", "dragend");
			}
		}else{
			this.fire("drag", ev, info);
		}
	};
	this.onTouchEnd = function(ev){
		if(this.dragging){
			this.fire("dragend", ev, this.lastInfo);
		}
		this.dragging = false;
	};
	this.isDragging = function(info){
		return (
			(this.horizontal && info.absDeltaX >= this.dragThreshold) ||
			(this.vertical && info.absDeltaY >= this.dragThreshold)
		);
	};
	/**
	 * Method to determine whether this Sortable is currently disabled.
	 * @return {Boolean} the disabled state of this Sortable.
	 */
	this.isVertical = function(){
		return this.vertical;
	};
	/**
	 * Method to determine whether this Sortable is currently sorting.
	 * @return {Boolean} the sorting state of this Sortable.
	 */
	this.isHorizontal = function(){
		return this.horizontal;
	};
});